package week8;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

public class Person {
	private String name;
	private int age;
	public Person(){}
	public Person(String n,int a)
	{
		this.name = n;
		this.age = a;
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public  byte[] objToArray(  ) throws Exception
	{
		ByteArrayOutputStream  baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		dos.writeUTF(name);
		dos.writeInt(age);
		baos.close();
		dos.close();
		return baos.toByteArray();
	}
	
	public static Person  ArrayToobj( byte[] b) throws Exception 
	{
		ByteArrayInputStream  bais = new ByteArrayInputStream(b);
		DataInputStream   dis = new DataInputStream(bais);
		Person  p = new Person();
		p.setName(dis.readUTF());
		p.setAge(dis.readInt());
		bais.close();
		dis.close();
		return p;
	}
}
